package com.sky.common.task;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.Map;
import java.util.concurrent.ScheduledFuture;
import java.util.function.Consumer;

@EnableScheduling
public class OnvifControlTask {

    private final ThreadPoolTaskScheduler taskScheduler;

    public OnvifControlTask() {
        this.taskScheduler = new ThreadPoolTaskScheduler();
        this.taskScheduler.initialize();
    }

    public ScheduledFuture<?> startMoveTask(Consumer<Map<String, Object>> task, long time, Map<String, Object> param) {
        return taskScheduler.scheduleWithFixedDelay(()->task.accept(param), time);
    }

    public ScheduledFuture<?> startMoveFocusTask(Consumer<Map<String, Object>> task, long time, Map<String, Object> param) {
        return taskScheduler.scheduleWithFixedDelay(()->task.accept(param), time);
    }

    public void stopTask(ScheduledFuture<?> scheduledFuture){
        scheduledFuture.cancel(true);
    }

    private void performTask(Map<String, Object> param) {
        System.out.println("定时任务执行时间: " + System.currentTimeMillis());
        for (String key : param.keySet()) {
            System.out.println(key+":"+param.get(key));
        }
    }

}
